<?php
namespace app\index\Lib;
class Weixin{
    private $corpid = "wx78421f21ad5398e1";
    //private $Secret = "NqmaVWKU_EBYflw8FForyw72J6xSc85m-fGbHykNyVQ";
    private $Secret = "VBtg6sr-RASByoUz4JAo8l-diZ7ptUCSPDnZBxxiNcESuaz3y4ihqktEgYWi2cwM";
    private $agentid = "1000004";
    public function __construct($corpid,$secret,$agentid){
        $this->corpid=$corpid;
        $this->Secret = $secret;
        $this->agentid = $agentid;
    }
    /**
     * 获取access_token
     */
    public function getAccessToken(){
        $access_token = cache("access_token");
        if (empty($access_token)){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
            $data = array('corpid'=>$this->corpid,"corpsecret"=>$this->Secret);
            $accessToken = sp_http($url, $data,'GET');
            $accessToken = json_decode($accessToken,true);
            cache("access_token",$accessToken['access_token'],array('type'=>'File','expire'=>$accessToken['expires_in']));
            $access_token = $accessToken['access_token'];
        }
        return $access_token;
    }
    
    /**
     * 生成回调url
     * @param unknown $url
     */
    public function OAuthUrl($url){
        $returnUrl = "https://open.weixin.qq.com/connect/oauth2/authorize";
        $param = [
           'appid'=>$this->corpid,
           'redirect_uri'=>$url,
           'response_type'=>'code',
           'scope'=>'snsapi_privateinfo',
           'agentid'=>$this->agentid
        ];
        $paramStr = http_build_query($param);
        $returnUrl .= "?".$paramStr."#wechat_redirect";
        return $returnUrl;
    }
    
    /**
     * 根据code获取成员信息
     * @param unknown $access_token
     * @param unknown $code
     * @return 成员userid信息
     */
    public function getUserInfo($code){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo";
        $params = array(
            'access_token'=>$this->getAccessToken(),
            'code'=>$code
        );
        $userInfo = sp_http($url, $params);
        $userInfo = json_decode($userInfo,true);
        return $userInfo;
    }
    
    /**
     * 使用user_ticket获取成员详情
     * @param unknown $user_ticket
     * @return mixed 用户详细信息数组
     */
   public function getUserDetail($user_ticket){
       $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token=".$this->getAccessToken();
       $params = array(
           'user_ticket'=>$user_ticket
       );
       $userDetail = sp_http($url, json_encode($params),'POST',[],true);
       return json_decode($userDetail,true);
   }
   
   
   public function getDeptList(){
       $url = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
       $params = [
           'access_token' => $this->getAccessToken(),
       ];
       $res = sp_http($url, $params);
       return json_decode($res);
   }
   /**
    * 根据userid获取员工详细信息
    * @param unknown $userid
    * @return mixed
    */
   public function getUserById($userid){
       $url = "https://qyapi.weixin.qq.com/cgi-bin/user/get";
       $params = [
           'access_token'=>$this->getAccessToken(),
           'userid'=>$userid
       ];
       $res = sp_http($url, $params);
       return json_decode($res);
   }
   
   
   public function getUseridByOpenid($userid){
       $url = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token=".$this->getAccessToken();
       $params = [
           'access_token' => $this->getAccessToken(),
           'userid'=>$userid
       ];
       $res = sp_http($url, json_encode($params),'POST',[],true);
       return json_decode($res);
   }
}